/**
 * 
 */
package us.mooresr.webtroopmanager.client.ui;


import java.util.List;

import us.mooresr.webtroopmanager.client.place.DashboardPlace;
import us.mooresr.webtroopmanager.client.place.EditEventPlace;
import us.mooresr.webtroopmanager.client.place.EventDriversPlace;
import us.mooresr.webtroopmanager.client.place.EventParticipantsPlace;
import us.mooresr.webtroopmanager.client.ui.cells.EventCell;
import us.mooresr.webtroopmanager.shared.EventSummary;
import us.mooresr.webtroopmanager.shared.Privilege;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

/**
 * @author christopher_moore
 *
 */
public class EventListViewImpl extends Composite implements EventListView {

	private static EventListViewUiBinder uiBinder = GWT
			.create(EventListViewUiBinder.class);

	interface EventListViewUiBinder extends UiBinder<Widget, EventListViewImpl> {
	}


	@UiField
	Button addButton;
	
	@UiField
	Button editButton;
	
	@UiField
	Button deleteButton;
	
	@UiField
	Button editParticipantsButton;
	
	@UiField
	Button editDriversButton;
	
	@UiField
	CheckBox showPastEvents;
	
	@UiField
	Button closeButton;
	
	@UiField
	Label statusLabel;
	
	@UiField
	ScrollPanel scrollPanel;

	@UiHandler("addButton")
	void onAddClick(ClickEvent e) {
		presenter.goTo(new EditEventPlace(null));
	}
	
	@UiHandler("editButton")
	void onEditClick(ClickEvent e) {
		presenter.goTo(new EditEventPlace(selectedEvent.getId()));
	}
	
	@UiHandler("deleteButton")
	void onDeleteClick(ClickEvent e) {
		presenter.deleteEvent(selectedEvent);
	}
	
	@UiHandler("closeButton")
	void onCloseClick(ClickEvent e) {
		presenter.goTo(new DashboardPlace());
	}
	
	@UiHandler("editParticipantsButton")
	void onEditParticipantsClick(ClickEvent e) {
		presenter.goTo(new EventParticipantsPlace(selectedEvent.getId()));
	}
	
	@UiHandler("editDriversButton")
	void onEditDrivers(ClickEvent e) {
		presenter.goTo(new EventDriversPlace(selectedEvent.getId()));
	}
	
	
	private CellList<EventSummary> cellList;
	private Presenter presenter;
	private EventSummary selectedEvent;
	private ListDataProvider<EventSummary> dataProvider;

	public EventListViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		
		ProvidesKey<EventSummary> keyProvider = new ProvidesKey<EventSummary>() {
			public Object getKey(EventSummary item) {
				return (item == null) ? null : item.getId();
			}
		};
		cellList = new CellList<EventSummary>(new EventCell(), keyProvider);

		cellList.addStyleName("listControl");
		
		final SingleSelectionModel<EventSummary> selectionModel = new SingleSelectionModel<EventSummary>(keyProvider);
		cellList.setSelectionModel(selectionModel);
		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

			@Override
			public void onSelectionChange(SelectionChangeEvent event) {
				selectedEvent = selectionModel.getSelectedObject();
				if (selectedEvent == null) {
					// Disable buttons that act on a selected event
					editButton.setEnabled(false);
					deleteButton.setEnabled(false);
					editParticipantsButton.setEnabled(false);
					editDriversButton.setEnabled(false);
	
				} else {
					// Enable buttons that act on a selected event
					if (presenter.hasPermission(Privilege.MODIFY_EVENT)) {
						editButton.setEnabled(true);
					}
					if (presenter.hasPermission(Privilege.DELETE_EVENT)){ 
						deleteButton.setEnabled(true);
					}
					editParticipantsButton.setEnabled(true);
					editDriversButton.setEnabled(true);

				}
				
				
			}
			
		});
		
		dataProvider = new ListDataProvider<EventSummary>();
		dataProvider.addDataDisplay(cellList);
		
		scrollPanel.setWidget(cellList);
		
		deleteButton.setEnabled(false);
		editButton.setEnabled(false);
		editParticipantsButton.setEnabled(false);
		editDriversButton.setEnabled(false);
		
		showPastEvents.addValueChangeHandler(new ValueChangeHandler<Boolean>() {

			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				Boolean newValue = event.getValue();
				
				presenter.showPastEventsChanged(newValue);
				
			}


			
		});
	}



	@Override
	public void addEvent(EventSummary e) {
		List<EventSummary> providerList = dataProvider.getList();
		
		providerList.add(e);
		
		// TODO: Resort?
		
		cellList.setPageSize(providerList.size());
		
	}

	@Override
	public void removeEvent(Long id) {
		List<EventSummary> providerList = dataProvider.getList();
		
		for (int i=0; i<providerList.size(); i++) {
			EventSummary es = providerList.get(i);
			if (es.getId() == id) {
				providerList.remove(i);
				break;
			}
		}
		
	}

	@Override
	public void setData(List<EventSummary> data) {

		List<EventSummary> providerList = dataProvider.getList();
		providerList.clear();
		for (EventSummary es : data) {
			providerList.add(es);
		}
		cellList.setPageSize(data.size());
		
	}

	@Override
	public void setStatus(String status) {
		statusLabel.setText(status);
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
		
		if (presenter.hasPermission(Privilege.ADD_EVENT)){
			addButton.setEnabled(true);
		} else {
			addButton.setEnabled(false);
		}
	}
}
